﻿package Interface
{	
	import caurina.transitions.Tweener;
	
	import Core.Page;		
	import Core.Game;
	import Core.InterfacePage;
	
	import flash.display.MovieClip;	
	import flash.events.Event;
	
	public class PagesInterface extends Page
	{
		public const INTERFACE_HIDED:String = 'INTERFACE_HIDED';
		//Pages
		private var FWelcomePage:PageWelcome;
		private var FShopPage:PageShop;
		private var FTracksPage:PageTracks;		
		private var FProfilePage:PageProfile;
		private var FBattlesPage:PageBattles;
		private var FTop100Page:PageTop100;		
		
		private var FMenu:UpMenu;
		//Pages storage
		private var FPages:Vector.<InterfacePage>;
		private var FCurrentPage:InterfacePage;
		
		public function PagesInterface() 
		{		
			FPages = new Vector.<InterfacePage>;
			
			FWelcomePage = AddPage('WelcomePage') as PageWelcome;
			FShopPage = AddPage('ShopPage') as PageShop;
			FTracksPage = AddPage('TracksPage') as PageTracks;
			FProfilePage = AddPage('ProfilePage') as PageProfile;			
			FTracksPage = AddPage('TracksPage') as PageTracks;
			FBattlesPage = AddPage('BattlesPage') as PageBattles;
			FTop100Page = AddPage('Top100Page') as PageTop100;
			
			FMenu = getChildByName('Menu') as UpMenu;		
			FMenu.visible = false;			
		}		
		
		private function AddPage(AName:String):InterfacePage
		{
			var vPage:InterfacePage;
			vPage = InterfacePage(getChildByName(AName));
			vPage.Interface = this;
			FPages.push(vPage);
			
			return vPage;
		}
		
		public function set CurrentPage(APage:InterfacePage):void
		{
			FCurrentPage = APage;
			FMenu.visible = FCurrentPage != FWelcomePage;
			if(FMenu.visible)
				FMenu.CurrentPage = FCurrentPage;
			
			for(var i in FPages)									
				FPages[i].visible = false;			
				
			FCurrentPage.visible = true;
		}
		
		override public function set GameCore(AGame:Game):void
		{
			super.GameCore = AGame;			
		}
		
		public function Hide():void
		{
			FGame.BlockInterface(false);
			Tweener.addTween(this, {alpha:0, time:1, transition: "easeOutQuint", onComplete:OnHide});
		}
		
		private function OnHide():void
		{			
			alpha = 1;
			visible = false;
			
			dispatchEvent(new Event(INTERFACE_HIDED));
		}
		
		public function FillMenu():void
		{
			FWelcomePage.FillMenu();
			FMenu.FillMenu(FGame.Pages);
		}
		
		public function get InterfaceMenu():UpMenu
		{
			return FMenu;
		}
	}	
}
